<?php

class PHPMock_Expectation_Director
{
    protected $_methodName = null;
    protected $_expectations = array();
    //protected $_defaults = array();
    //protected $_expectationOrder = null;
    
    public function __construct($methodName)
    {
        $this->_methodName = $methodName;
    }
    
    public function addExpectation(PHPMock_Expectation $expectation)
    {
    	$this->_expectations[] = $expectation;
    }
    
    public function verify()
    {
        foreach ($this->_expectations as $expectation) {
            $expectation->verify();
        }
    }
    
    /**
     * @throws PHPMock_Exception if method call is out of order
     */
    public function call(array $args, $mock)
    {
        $expectation = $this->findExpectation($args);
        if (!empty($expectation)) {
            if ($expectation->isOrdered()) {
                $mock->incrementOrderedNumber();
            }
        	$return = $expectation->verifyCall($args);
        	return $return;
        } else {
            throw new PHPMock_Exception('unable to find a matching expectation for ' . $this->_methodName);
        }
    }
    
    public function findExpectation(array $args)
    {
        foreach ($this->_expectations as $expectation) {
            if ($expectation->matchArgs($args)) {
            	return $expectation;
            }
        }
    }
    
}